package org.seefly.iprint.controller;

import lombok.Setter;
import org.seefly.iprint.entity.User;
import org.seefly.iprint.service.UserService;
import org.seefly.iprint.utils.CheckCodeUtil;
import org.seefly.iprint.vo.LoginUserVo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @ClassName LoginController
 * @Description 用户登陆注册模块
 * @author liujianxin
 * @Date 2018年3月24日 下午4:31:03
 * @version 1.0.0
 */
@Controller
@ConfigurationProperties(prefix = "user.login")
@Setter
public class LoginController {

    private UserService service;
    public LoginController(UserService service) {
        this.service = service;
    }

    /**登陆成功*/
    private String success;
    /**账号或密码错误*/
    private String accountOrPwdError;
    /**验证码错误*/
    private String codeError;
    /**登陆状态 已登陆*/
    private String alreadyLogin;
    /**登陆状态 未登录*/
    private String notLogin;
    /**
     * 获取登陆页面
     * @return 页面地址
     */
    @RequestMapping("/login")
    public String login(Model m) {
        m.addAttribute("type", "login");
        return "front/login";
    }

    /**
     * 登陆验证
     */
    @RequestMapping("/doLogin")
    @ResponseBody
    public String doLogin(@RequestBody LoginUserVo vo, HttpSession session) {
        String loginCode = (String) session.getAttribute("loginCode");
        String msg = null;
        // 判断验证码
        if (loginCode != null && loginCode.equalsIgnoreCase(vo.getCode())) {
            // 验证码正确，判断用户是否存在
            User user = service.login(vo);
            if (user != null) {
                // 用户名密码正确,将用户实体放入session中
                session.setAttribute("user", user);
                msg = success;
            } else {
                // 用户名密码不正确
                msg = accountOrPwdError;
            }
        } else { 
            // 验证码不正确
            msg = codeError;
        }
        // 移除session中的验证码
        session.removeAttribute("loginCode");
        return msg;
    }

    /**
     * 获取登陆验证码
     * @param session 存放验证码值
     * @param response 异步响应图片io流
     */
    @RequestMapping("/loginCode")
    public void returnLoginCodeImg(HttpSession session, HttpServletResponse response) throws IOException {
        // 创建一张内存图片
        int width = 30 * 4;
        int height = 30;
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        String msg = CheckCodeUtil.getCode(bufferedImage);
        // 写入session
        session.setAttribute("loginCode", msg);
        // 图片输出 ImageIO
        ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
    }

    /**
     * 
     * 用户退出操作，重定向到首页
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:index";
    }

    /**
     * 选择商品时异步判断用户是否登陆
     * @return 是否登陆信息
     */
    @RequestMapping("/isLogin")
    @ResponseBody
    public String isLogin(HttpSession session) {
        Object user = session.getAttribute("user");
        return user == null ? notLogin : alreadyLogin;
    }
}
